<?php
namespace Duobao\Controller;

use Think\Controller;
use Com\Weixin\WeixinApi as WeixinApi;

class MenuController extends Controller{
    protected $menu = '';
    protected $url = '';

    public function __construct()
    {
        parent::__construct();
        #定义菜单
        $this->menu = array(
            "button"=>array(
                    array(
//                        'type'=>'view',
//                        'name'=>'新手引导',
//                        'url'=> 'http://api2.shaoyangduobao.weizhuanqiandao.com/User/wxLogin?act=newbie'
                        "name"=>'新手引导',
                        "sub_button"=>array(
                            array(
                                'type'=>'view',
                                "name"=>"服务协议",
                                "url"=>C('APP_URL')."/User/wxLogin2?act=agreement"
                            ),
                            array(
                                'type'=>'view',
                                "name"=>"常见问题",
                                "url"=>C('APP_URL')."/User/wxLogin2?act=qa"
                            ),
                            array(
                                'type'=>'view',
                                "name"=>"购物指南",
                                "url"=>C('APP_URL')."/User/wxLogin2?act=guide"
                            )
                        )
                    ),
                    array(
                        'type'=>'view',
                        'name'=>'夺宝首页',
                        'url'=> C('APP_URL').'/User/wxLogin2?act=index'
                    ),
                    array(
                        'type'=>'view',
                        'name'=>'我的夺宝',
                        'url'=> C('APP_URL').'/User/wxLogin2?act=user'
                    )
            ),
        );

        #获取post url
        $this->url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.(new WeixinApi())->getToken();
    }

    /**
     * 默认
     */
    public function index(){
        $config = M('config');

        $condition['name'] = 'menu_switch';
        $switchVal = $config->where($condition)->getField('value');#获取微信的菜单的开关

        if($switchVal == 1){
            $result = https_request($this->url,json_encode($this->menu,  JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ));#让json编码不对unicode编码和斜杠编码
            echo $result;
        }else{
            echo "{\"errcode\":110,\"errmsg\":\"Create weixin custom menu button switch is off\"}";#数据库中的创建微信自定义菜单的开关是关闭的状态
        }
    }


    /**
     * 查询创建的菜单
     */
    public function queryMenu(){
        $url = 'https://api.weixin.qq.com/cgi-bin/menu/get?access_token='.(new WeixinApi())->getToken();
        $result = https_request($url);
        echo $result;
    }

    /**
     * 删除自定义菜单不做定义
     */


    /*end menu controller*/
}